package org.excel;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.List;
import java.util.Map;

import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;

public class App {

	/*public static void maina(String[] args) throws Exception {
		String sql = "CREATE TABLE `bpm_archiveddata` (   `WF_OrUnid` varchar(100) NOT NULL DEFAULT '',   `SUBJECT` text,   `WF_Appid` varchar(30) DEFAULT NULL,   `WF_Processid` varchar(50) DEFAULT NULL,   `WF_ProcessName` varchar(200) DEFAULT NULL,   `WF_AddName` varchar(30) DEFAULT NULL,   `WF_AddName_CN` varchar(50) DEFAULT NULL,   `WF_Author` text,   `WF_Author_CN` text,   `WF_EndUser` text,   `WF_AllReaders` text,   `WF_CopyUser` text,   `WF_DocNumber` varchar(30) DEFAULT NULL,   `WF_CurrentNodeid` varchar(100) DEFAULT NULL,   `WF_CurrentNodeName` text,   `WF_Status` varchar(30) DEFAULT NULL,   `WF_VersionNumber` varchar(30) DEFAULT NULL,   `WF_SourceEntrustUserid` varchar(200) DEFAULT NULL,   `WF_TargetEntrustUserid` varchar(200) DEFAULT NULL,   `WF_ProcessNumber` varchar(30) DEFAULT NULL,   `WF_MainNodeid` varchar(50) DEFAULT NULL,   `wf_maindocunid` varchar(100) DEFAULT NULL,   `TopDocUnid` varchar(100) DEFAULT NULL,   `WF_TotalTime` varchar(10) DEFAULT NULL,   `WF_EndTime` varchar(30) DEFAULT NULL,   `WF_BusinessNum` varchar(50) DEFAULT NULL,   `WF_EndBusinessid` varchar(30) DEFAULT NULL,   `WF_Folderid` varchar(30) DEFAULT NULL,   `WF_TransFlag` varchar(30) DEFAULT 'N',   `WF_ArcFormNumber` varchar(30) DEFAULT NULL,   `WF_AddDeptid` varchar(50) DEFAULT NULL,   `WF_Systemid` varchar(30) DEFAULT NULL,   `WF_DocCreated` varchar(30) DEFAULT NULL,   `WF_LastModified` varchar(50) DEFAULT NULL,   `XmlData` text,   `TopProcessid` varchar(50) DEFAULT NULL,   `URGENTLEVEL` varchar(50) DEFAULT NULL,   `flowcode` varchar(50) DEFAULT NULL,   `old_url` text,   `WF_TopProcessName` varchar(100) DEFAULT NULL,   `WF_AddFolderName` varchar(50) DEFAULT NULL,   `WF_AddTopFolderName` varchar(50) DEFAULT NULL,   `WF_AddTopDeptid` varchar(50) DEFAULT NULL,   `log_is_host_dept` varchar(1) DEFAULT '0' COMMENT '0普通1主办2协办3阅示',   `log_host_flag` varchar(1) DEFAULT '0' COMMENT '0不是1一级主办',   PRIMARY KEY (`WF_OrUnid`),   KEY `index_archiveddata_docnid` (`TopDocUnid`) USING BTREE,   KEY `idx_arch_maindocunid` (`wf_maindocunid`),   KEY `WF_DOCCREATED1` (`WF_DocCreated`) USING BTREE,   KEY `idx_arch_wfunid` (`WF_OrUnid`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
		MySqlStatementParser parser = new MySqlStatementParser(sql);
		SQLStatement statement = parser.parseStatement();
		MySqlCreateTableStatement create = (MySqlCreateTableStatement) statement;
		List<SQLTableElement> tableElementList = create.getTableElementList();
		for (SQLTableElement sqlTableElement : tableElementList) {
			if (sqlTableElement instanceof SQLColumnDefinition) {
				SQLColumnDefinition def = (SQLColumnDefinition) sqlTableElement;
				String colName = def.getName().getSimpleName().replace("`", "");
				System.out.print("{\""+colName+"\",\"");
				if (def.getComment() != null && !"".equals(def.getComment())) {
					System.out.println(def.getComment().toString().replaceAll("^'(\\s\\S)'$", "$1")+"\"},");
				} else {
					System.out.println(colName+"\"},");
				}
			}
		}

		SQLExprTableSource source = create.getTableSource();
		String tableName = source.toString();
//		System.err.println(tableName);
	}*/

	public static void main(String[] args) throws Exception {
		String[][] header = new String[][]{
			{"WF_ORUNID","WF_OrUnid","10"},
			{"SUBJECT","SUBJECT"},
			{"WF_Appid","WF_Appid"},
			{"WF_Processid","WF_Processid"},
			{"WF_ProcessName","WF_ProcessName"},
			{"WF_AddName","WF_AddName"},
			{"WF_AddName_CN","WF_AddName_CN"},
			{"WF_Author","WF_Author"},
			{"WF_Author_CN","WF_Author_CN"},
			{"WF_EndUser","WF_EndUser"},
			{"WF_AllReaders","WF_AllReaders"},
			{"WF_CopyUser","WF_CopyUser"},
			{"WF_DocNumber","WF_DocNumber"},
			{"WF_CurrentNodeid","WF_CurrentNodeid"},
			{"WF_CurrentNodeName","WF_CurrentNodeName"},
			{"WF_Status","WF_Status"},
			{"WF_VersionNumber","WF_VersionNumber"},
			{"WF_SourceEntrustUserid","WF_SourceEntrustUserid"},
			{"WF_TargetEntrustUserid","WF_TargetEntrustUserid"},
			{"WF_ProcessNumber","WF_ProcessNumber"},
			{"WF_MainNodeid","WF_MainNodeid"},
			{"wf_maindocunid","wf_maindocunid"},
			{"TopDocUnid","TopDocUnid"},
			{"WF_TotalTime","WF_TotalTime"},
			{"WF_EndTime","WF_EndTime"},
			{"WF_BusinessNum","WF_BusinessNum"},
			{"WF_EndBusinessid","WF_EndBusinessid"},
			{"WF_Folderid","WF_Folderid"},
			{"WF_TransFlag","WF_TransFlag"},
			{"WF_ArcFormNumber","WF_ArcFormNumber"},
			{"WF_AddDeptid","WF_AddDeptid"},
			{"WF_Systemid","WF_Systemid"},
			{"WF_DocCreated","WF_DocCreated"},
			{"WF_LastModified","WF_LastModified"},
			{"XmlData","XmlData"},
			{"TopProcessid","TopProcessid"},
			{"URGENTLEVEL","URGENTLEVEL"},
			{"flowcode","flowcode"},
			{"old_url","old_url"},
			{"WF_TopProcessName","WF_TopProcessName"},
			{"WF_AddFolderName","WF_AddFolderName"},
			{"WF_AddTopFolderName","WF_AddTopFolderName"},
			{"WF_AddTopDeptid","WF_AddTopDeptid"},
			{"log_is_host_dept","'0普通1主办2协办3阅示'"},
			{"log_host_flag","'0不是1一级主办'"},
		};
		MySQL mySQL = new MySQL("132.120.115.125", "13306", "yth_bpm", "yth_bpm_junnan", "Mygod_You!L0ve@Me?");
		Connection conn = mySQL.getConnection();
		long b = System.currentTimeMillis();
		ExcelExportUtil.export(args[0], "excel", header, "sheet1", 20000, conn, "select * from bpm_archiveddata");
		conn.close();
		System.out.println("Excel file export is success done ..." + (System.currentTimeMillis() - b));
	}
}
